var __Browser={};
var domTimer;
var http_request;
var ajaxUrl = "ajaxInterface.php?";


(function getBrowserAgent(){
	var agent = navigator.userAgent;
	__Browser.IE = !!agent.match(/MSIE/);
	__Browser.FF = agent.match(/Mozilla/i)&&!agent.match(/compatible|WebKit/i);
	__Browser.Opera = !!agent.match(/Opera/i);
	__Browser.Safari = !!agent.match(/Mac|Safari/i);
})();


function $addEvent(obj , evt , fn){
	if(__Browser.IE){
		obj.attachEvent("on"+evt , fn);
	}
	else{
		obj.addEventListener(evt , fn ,false);
	}
}

function $(id){
	if(!id) return false;
	return document.getElementById(id);
}

function $t(id){
	return document.getElementsByTagName(id);
}

function __domready(){
	if(__Browser.FF){
		$addEvent(document , "DOMContentLoaded" , __init)
	}
	if(__Browser.IE){
		domTimer = setInterval(function(){
			try{
				document.body.doScroll('right');
				clearInterval(domTimer)
				__init();
			}catch(e){};
		});
	}
}

function getSrcEl(e){
	var evt = window.event || e;
	var src = evt.target || evt.srcElement;
	return src;
}

function createXMLHTTPObject(){
	var XMLHttpFactories = [
		function() {return new XMLHttpRequest();},
		function() {return new ActiveXObject('Msxml2.XMLHTTP');},
		function() {return new ActiveXObject('Msxml3.XMLHTTP');},
		function() {return new ActiveXObject('Microsoft.XMLHTTP');}
	];
	var xmlhttp = false;
	for(var i=0;i<XMLHttpFactories.length;i++){
		try{
			xmlhttp = XMLHttpFactories[i]();
		}
		catch(e){
			continue;
		}
	}
	return xmlhttp;
}

function makeAjaxRequest(url , data , sys , callback){
	http_request = createXMLHTTPObject();
	 if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
     }

	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
		 if (http_request.status == 200) {
			callback();
		 }
	  }
	}
	http_request.open('POST', url, sys);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	if(data) { http_request.setRequestHeader("Content-length", data.length); }
	http_request.setRequestHeader("Connection", "close");
	http_request.send(data);
}

function addFn(){
	var text = http_request.responseText;
	var rdata = eval("("+text+")");
	var uauth = rdata.auth==1?"Administrator":"ComUser";
	
	var newdata = [rdata.id , rdata.name , rdata.pwd , rdata.nick , uauth];
	var len = newdata.length;
	var newTr = document.createElement("tr");
	newTr.style.backgroundColor = "#DDDDDD";
	newTr.setAttribute("tag" , rdata.id);
	
	var newUp = document.createElement('input');
	newUp.type="button";
	newUp.className = "login_submit3";
	newUp.value="更新";
	newUp.setAttribute("userid" , rdata.id);
	newUp.setAttribute("tag" , "update");
	
	var newDel = document.createElement('input');
	newDel.type="button";
	newDel.className = "login_submit3";
	newDel.value="删除";
	newDel.setAttribute("userid" , rdata.id);
	newDel.setAttribute("tag" , "delete");

	var newT = document.createElement("text");
	newT.innerHTML ="&nbsp;&nbsp;";
	newTr.appendChild(newUp);
	newTr.appendChild(newT);
	newTr.appendChild(newDel);

	$addEvent(newUp , "click" , setActionWindow);
	$addEvent(newDel , "click" , setActionWindow);

	for(var i=0;i<len;i++){
		var newTd = document.createElement("td");
		newTd.innerHTML = newdata[i];
		newTr.appendChild(newTd);
	}
	$('userList').getElementsByTagName('tbody')[0].appendChild(newTr);

	hideAddWindow();
}

function delFn(){
	var text = http_request.responseText;
	var trs = $('userList').getElementsByTagName('tr');
	for(var i=2 , l=trs.length;i<l;i++){
		if(trs[i].getAttribute("tag")==text)	
			$('userList').getElementsByTagName('tbody')[0].removeChild(trs[i]);
	}
}

function __init(){
	//$addEvent($('addBtn') , 'mousedown' , showAddWindow);
	$addEvent($('saveBtn') , 'click' , ajaxAddUser);
	$addEvent($('cancelBtn') , 'mousedown' ,hideAddWindow);
	
	$addEvent($('userList') , "click" , setActionWindow);
}

function setActionWindow(e){
	var el = getSrcEl(e);
	if(el.tagName=="INPUT"){
		var action=el.getAttribute("tag");
		var userid = el.getAttribute("userid");
		if(action=="add"){
			showAddWindow();
		}
		if(action=="update"){
			alert("正在开发中......(12/05/08)");
			//showUpdateWindow();
		}
		if(action=="delete"){
			ajaxDeleteUser(userid);
		}
	}
}

function showAddWindow(){
	$('mask').style.height = window.innerHeight + 'px';
	$('mask').style.width = window.innerWidth + 'px';


	$('newDiv').style.display = "block";
	$('newDiv').style.left = (window.innerWidth/2-200)+'px';
	$('newDiv').style.top = (window.innerHeight/2)+'px';
}

function hideAddWindow(){
	$('mask').style.height = '0px';
	$('mask').style.width = '0px';
	$('newDiv').style.display = "none"
}

function ajaxAddUser(){
	var action = "action=adduser";
	var username = "&username="+encodeURI($('username').value);
	var userpwd = "&userpwd="+encodeURI($('userpwd').value);
	var userauth = "&userauth="+encodeURI($('userauth').value);
	var usernick = "&usernick="+encodeURI($('usernick').value);
	var param = action+username+userpwd+userauth+usernick;
	makeAjaxRequest(ajaxUrl , param , true , addFn);
}

function ajaxDeleteUser(uid){
	var action = "action=deluser";
	var userid = "&userid="+encodeURI(uid);
	var param = action+userid;
	makeAjaxRequest(ajaxUrl , param , true , delFn);
}





//xmlhttp.open(method , url ,true);

__domready();